﻿@page "/xlsio/formulas"
@inject Microsoft.JSInterop.IJSRuntime JS
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment
@using System.IO;

@using Syncfusion.Blazor.Buttons
@*Hidden:Lines*@

@using BlazorDemos.Data.FileFormats.XlsIO

@*End:Hidden*@
@inherits SampleBaseComponent;

<SampleDescription>
  <p>This sample demonstrates how to use formulas in spreadsheets using XlsIO.</p>  
</SampleDescription>
<ActionDescription>
<p>Formulas are entries in Excel that have an equation that calculates the value to be displayed. A typical formula might contain cells, constants, and even functions. Essential XlsIO has support for reading and writing formulas.</p>
<p style='display: block'><b>Features:</b></p>
<ul>
<li>Writing array formulas</li>
<li>Writing Excel built-in formulas</li>
<li>Reading formula strings using XlsIO</li>
<li>Reading evaluated formulas using XlsIO</li>
</ul> 
</ActionDescription>

<div class="control-section">
    <p style="font-weight:normal">Click the button to view an Excel spreadsheet generated by Essential XlsIO. Please Read Formula from the template document.</p>
    
    <div class="button-section">
        <div id="button-control">
            <div class="row">
                <label style="font-weight:normal">
                    Computed Value
                </label>
                &nbsp;&nbsp;&nbsp;
                <label id="computedvalue" style="font-weight:normal">
                    @computedValue
                </label>
            </div>
            <div class="row">
                <label style="font-weight:normal">
                    Formula String
                </label>
                &nbsp;&nbsp;&nbsp;
                <label id="computedvalue" style="font-weight:normal">
                    @formulaString
                </label>
            </div>
            <div class="row">
                <div>
                    <SfButton @onclick="ReadFormula">Read Formula</SfButton>&nbsp;
                    <SfButton @onclick="WriteFormula">Write Formula</SfButton>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {
    MemoryStream excelStream;
    string button = string.Empty;
    string computedValue = string.Empty;
    string formulaString = string.Empty;

    /// <summary>
    /// Retrive the formulas in Excel document
    /// </summary>
    protected void ReadFormula()
    {
        FormulasService service = new FormulasService(hostingEnvironment);
        button = "Read Formula";
        excelStream = service.FormulasXlsIO(button);
        computedValue = service.ViewData["computedValue"].ToString();
        formulaString = service.ViewData["formulaString"].ToString();
        excelStream.Dispose();
    }

    /// <summary>
    /// Calculate the formulas in Excel document
    /// </summary>
    public async Task WriteFormula()
    {
        FormulasService service = new FormulasService(hostingEnvironment);
        button = "Write Formula";
        excelStream = service.FormulasXlsIO(button);
        await JS.SaveAs("Sample.xlsx", excelStream.ToArray());
    }
}
